﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Framework.Extensions
{
    public static class TypeExtensions
    {
        public static IEnumerable<Type> GetImplementedTypes(this Type type)
        {
            return type
                .Chain(p => p.BaseType)
                .Concat(type.GetInterfaces())
                .Skip(1);
        }

        public static IEnumerable<Type> GetArgumentsOfGenericBaseType(this Type type, Type compareType)
        {
            var baseTypeDefinition = type
                .GetImplementedTypes()
                .FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == compareType);

            return baseTypeDefinition != null
                       ? baseTypeDefinition.GetGenericArguments()
                       : new Type[] {};
        }
    }
}